# -*- coding:utf-8 -*-
# @Author:Cheng Lei 1037654919@qq.com
# @Time : 2023/3/23 下午2:21
# @FileName: kuwo_t2_playlist_song.py
# @Software: PyCharm
# 第二步 获取所有歌单歌曲对应关系


from multiprocessing import Pool
from class_kuwo import Kuwo
kuwo = Kuwo()
from utils import MongoDBUtil, mongo_manager
MongoDBUtil = MongoDBUtil()
playlist_tag = mongo_manager("playlist_tag", db="kuwo")


# 插入数据
def insert_data(tablename, datas):
    if len(datas) > 0 and datas is not None:
        for data in datas:
            # data['_id'] = data['id']
            try:
                # print(data)
                MongoDBUtil.insert_one(tablename, data)
            except Exception as e:
                print(e)
    else:
        pass


# 解析歌单信息
def parse_playlist_info(play_list_info):
    datas = play_list_info

    results = []
    result = {'_id': datas.get("id"), 'id': datas.get("id"),
              'img': datas.get("img700"), 'info': datas.get("info"),
              'isOfficial': datas.get("isOfficial"), 'listencnt': datas.get("listencnt"),
              'name': datas.get("name"),'tag': datas.get("tag"),
              'total': datas.get("total"), 'uPic': datas.get("uPic"),
              'uname': datas.get("uname"), 'userName': datas.get("userName")}

    musiclist = datas.get("musicList")
    for music in musiclist:
        result2 = {'_id': str( datas.get("id")) + '_' + str(music.get("rid")),
                   'playlist_id': datas.get("id"),
                   'musicrid': music.get("musicrid"), 'rid': music.get("rid"),
                   'albumid': music.get("albumid"), 'albumpic': music.get("albumpic")
                   }
        results.append(result2)
    return result, results


# 获取歌单信息
def get_playlist_info(seed):
    playlist_tag = mongo_manager("playlist_tag", db="kuwo")
    page = 1
    datas = kuwo.get_play_list_info(seed['id'], page, 20)
    result, results = parse_playlist_info(datas)
    insert_data(tablename='playlist_song', datas=results)
    while len(datas) == 20:
        try:
            page = page + 1
            print("begin page : ", page)
            datas = kuwo.get_play_list_info(seed['id'], page, 20)
            result, results = parse_playlist_info(datas)
            insert_data(tablename='playlist_song', datas=results)
        except:
            break
    seed['status'] = 'success'
    seed['tags'] = result['tag']
    seed['info'] = result['info']
    seed['isOfficial'] = result['isOfficial']
    playlist_tag.updateOne({'_id': seed['_id']}, seed)
    playlist_tag.close()


# 获取歌单信息 run
def get_playlist_info_run():
    pool = Pool(processes=32)
    seeds = playlist_tag.findAll({"status": None})


    lists =[]
    for seed in seeds:
        lists.append(seed)
    print(len(lists))
    playlist_tag.close()

    pool.map(get_playlist_info, lists)
    pool.close()  # 关闭进程池，不再接受新的进程
    pool.join()


if __name__ == '__main__':
    # test
    # data=''
    # page = 1
    # id=[3453950594]
    # datas = kuwo.get_play_list_info(id, page, 20)
    # result, results = parse_playlist_info(datas)
    # print(result)
    # print(results)
    # # insert_data(tablename='playlist_song', datas=results)
    # playlist_tag= mongo_manager("playlist_tag", db="kuwo")
    # for pl in playlist_tag.findAll({"status":"success"}):
    #     pl["status"] = None
    #     playlist_tag.updateOne({"_id":pl["_id"]},pl)

    # main
    get_playlist_info_run()









